var request = require('superagent-charset');
var superagent = require('superagent');
var cheerio = require('cheerio');
var async = require("async");
var fs = require("fs");
var mysql = require("mysql");
request(superagent);

//连接数据库
var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'root',
    database: 'node_api',
});
connection.connect();

var arr=[];//电影详细页链接
var url = "http://www.ttmeiju.com/latest.html";
superagent.get(url).end(function(error, mes) {
    if (error) {
        console.log("出问题了1:" + error);
    } else {
        //created_at
        var getDate=new Date();
        var day=getDate.toLocaleDateString();
        console.log(day)
        var $ = cheerio.load(mes.text);
        var usa_tv_name,
            usa_tv_logo,
            usa_tv_public_time,
            usa_tv_desc,
            usa_tv_download_name,
            usa_tv_baiduyun_link,
            usa_tv_baiduyun_password,
            usa_tv_bt_link,
            usa_tv_xunlei_link,
            usa_tv_ed2k_link,
            usa_tv_mi_link,
            usa_tv_size

        $(".latesttable tr").each(function(){

            var _url="http://www.ttmeiju.com/"+$(this).find('a').eq(1).attr('href')
            var aa=$(this).find('a').eq(1).attr('href')
            var bb=String(aa).split('/')[2]
            usa_tv_number=String(bb).split('.')[0]
            usa_tv_baiduyun_password=$(this).find('td').eq(3).html()
            //console.log(usa_tv_baiduyun_password)
            usa_tv_name=$(this).find('td').eq(1).text().replace(/(^\s*)|(\s*$)/g,"")
            //console.log(usa_tv_name)
            var link=$(this).find('td').eq(2).html()
            //console.log(link)
            $(this).find('td').eq(2).find('a').each(function(){

                if($(this).attr('title').indexOf('百度')>=0){
                    usa_tv_baiduyun_link=$(this).attr('href')
                }else if($(this).attr('title').indexOf('迅雷')>=0){
                    usa_tv_xunlei_link=$(this).attr('href')
                }else if($(this).attr('title').indexOf('小米')>=0){
                    usa_tv_mi_link=$(this).attr('href')
                }else if($(this).attr('title').indexOf('BT')>=0){
                    usa_tv_bt_link=$(this).attr('href')
                }else if($(this).attr('title').indexOf('ed2k')>=0){
                    usa_tv_ed2k_link=$(this).attr('href')
                }else if($(this).attr('title').indexOf('其他')>=0){
                    usa_tv_download_name=$(this).attr('href')
                }
            })

            var userinto = 'INSERT INTO usa_tvs(usa_tv_name,usa_tv_download_name,usa_tv_baiduyun_link,usa_tv_baiduyun_password,usa_tv_bt_link,usa_tv_xunlei_link,usa_tv_ed2k_link,usa_tv_mi_link) VALUES(?,?,?,?,?,?,?,?)';
            var userinto_Params = [usa_tv_name,usa_tv_download_name,usa_tv_baiduyun_link,usa_tv_baiduyun_password,usa_tv_bt_link,usa_tv_xunlei_link,usa_tv_ed2k_link,usa_tv_mi_link];
            connection.query(userinto, userinto_Params, function(err, result) {
                if (err) {
                    console.log('[ERROR] - ', err.message);
                    return;
                } else {
                    console.log("usa_tvs");
                }
            });

            superagent.get(_url).end(function(error, res,path) {
                if (error) {
                    console.log("出问题了1:" + error);
                } else {
                    var $$ = cheerio.load(res.text);
                    usa_tv_logo=$$('.seedpic img').attr('src')
                    usa_tv_desc=$$('.newstxt').text()
                    //request Request  path _headers
                    $$('.links li').each(function(){
                        if($(this).text().indexOf('名字')>=0){
                            title=$(this).text().substring(3)
                        }else if($(this).text().indexOf('发布时间')>=0){
                            usa_tv_public_time=$(this).text().substring(5)
                        }else if($(this).text().indexOf('大小')>=0){
                            usa_tv_size=$(this).text().substring(3)
                        }
                    })

                    var userinto = 'UPDATE usa_tvs set usa_tv_desc="'+usa_tv_desc+'",usa_tv_size="'+usa_tv_size+'",usa_tv_public_time="'+usa_tv_public_time+'",usa_tv_logo="'+usa_tv_logo+'" where usa_tv_name="'+title+'"';
                    
                    connection.query(userinto, function(err, result) {
                        if (err) {
                            console.log('[ERROR] - ', err.message);
                            return;
                        } else {
                            console.log("修改成功");
                        }
                    });

                    
                };
            });


            
            

        });
        
    };
});
